﻿
using Red.Controls.Shell;
using Red.Controls.ViewModels;
using Red.EngineData.View;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Data;
using Red.Controls.Converters;

namespace Red.Controls.DataDriven
{
    /// <summary>
    /// 
    /// </summary>
    [InteropControl( "Color" )]
    public partial class ColorPickerDropDown : DropDown
    {
        #region Data

        private bool _internalSet = false;

        #endregion Data

        #region Properties

        public static readonly DependencyProperty ColorProperty =
            DependencyProperty.Register( "Color", typeof( Color ), typeof( ColorPickerDropDown ), new FrameworkPropertyMetadata( Color.FromRgb( 255, 255, 255 ) ) );

        /// <summary>
        ///     Currently selected color
        /// </summary>
        public Color Color
        {
            get { return (Color)GetValue( ColorProperty ); }
            set { SetValue( ColorProperty, value ); }
        }

        #endregion Properties

        #region C-tor

        public ColorPickerDropDown()
        {
            InitializeComponent();
            Panel.DataContext = this;
            
            SetBinding( IsEnabledProperty, new Binding( "IsReadOnly" )
            {
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                Converter = new BoolInverter()
            } );
        }

        #endregion C-tor

        #region Event handlers

        protected async override void OnPropertyChanged( DependencyPropertyChangedEventArgs e )
        {
            base.OnPropertyChanged( e );

            if ( e.Property == ColorProperty )
            {
                var value = (Color)e.NewValue;
                var dataItem = DataContext as InteropDataItem;
                if ( dataItem != null && !_internalSet )
                { // push to Interop
                    await dataItem.SetSubValueAsync( "Red", value.R );
                    await dataItem.SetSubValueAsync( "Green", value.G );
                    await dataItem.SetSubValueAsync( "Blue", value.B );
                    await dataItem.SetSubValueAsync( "Alpha", value.A );
                }
            }
            else if ( e.Property == DataContextProperty )
            {
                var dataItem = e.NewValue as InteropDataItem;
                if ( dataItem != null )
                {
                    byte r = await dataItem.GetSubValueAsync<byte>( "Red" );
                    byte g = await dataItem.GetSubValueAsync<byte>( "Green" );
                    byte b = await dataItem.GetSubValueAsync<byte>( "Blue" );
                    byte a = await dataItem.GetSubValueAsync<byte>( "Alpha" );
                    
                    _internalSet = true;
                    Color = Color.FromArgb( a, r, g, b );
                    _internalSet = false;
                }
            }
        }

        private void DropDown_PanelClosed( object sender, DropDown.PanelClosedEventArgs e )
        {
            if ( e.Result == false )
            {
                Color = _picker.OriginalColor;
            }
        }

        #endregion Event handlers
    }
}
